package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Component("1002")
public class TrackStrategy implements ItemTypeStrategy {
    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;
    @Resource
    private TrackInfoFeignClient trackInfoFeignClient;


    /**
     * 处理购买项目类型为：声音
     * @param userPaidRecordVo
     */
    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("处理购买项目类型为：声音");
        //  声音 user_paid_track; 判断当前购买的声音Id在当前表中是否存在！
        //  用户要购买的声音列表;
        List<Long> itemIdList = userPaidRecordVo.getItemIdList();
        //  用户已购买的声音列表;
        LambdaQueryWrapper<UserPaidTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPaidTrack::getUserId, userPaidRecordVo.getUserId())
                .in(UserPaidTrack::getTrackId, itemIdList);
        //  用户在已购买的列表中买了对应的声音：
        Set<Long> paidTrackIdSet = userPaidTrackMapper.selectList(wrapper).stream().map(UserPaidTrack::getTrackId).collect(Collectors.toSet());
        //  需要将这个集合变为set集合; 通过trackId 判断是否存在;
        //  根据声音Id获取声音对象;
        Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackInfo(userPaidRecordVo.getItemIdList().get(0));
        Assert.notNull(trackInfoResult, "查询声音信息失败");
        TrackInfo trackInfo = trackInfoResult.getData();
        Assert.notNull(trackInfo, "查询声音信息失败");
        //  没有买过;
        //  判断：
        if (!CollectionUtils.isEmpty(paidTrackIdSet)) {
            //  有买过
            for (Long itemId : itemIdList) {
                //  判断是否包含;
                if (paidTrackIdSet.contains(itemId)) {
                    //  continue; break; return;
                    //  需要将包含的声音添加到集合中，用来记录哪些声音属于购买过!
                    //  需要使用mq进行增加余额;
                    continue;
                }
                //  创建一个对象：
                UserPaidTrack userPaidTrack = new UserPaidTrack();
                userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
                userPaidTrack.setUserId(userPaidRecordVo.getUserId());
                userPaidTrack.setAlbumId(trackInfo.getAlbumId());
                userPaidTrack.setTrackId(itemId);
                //  保存数据;
                userPaidTrackMapper.insert(userPaidTrack);
            }
            return;
        }
        for (Long trackId : itemIdList) {
            //  创建一个对象：
            UserPaidTrack userPaidTrack = new UserPaidTrack();
            userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
            userPaidTrack.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack.setAlbumId(trackInfo.getAlbumId());
            userPaidTrack.setTrackId(trackId);
            //  保存数据;
            userPaidTrackMapper.insert(userPaidTrack);
        }
    }
}
